<template>
    <div id="login">
        <div class="login-top">
            <h2>欢迎登录xx游戏</h2>
        </div>
        <div class="login-form">
            <van-field v-model="user.username" center label="用户名" placeholder="请输入用户名" label-align="top" />
            <van-field v-model="user.password" center type="password" label="密码" placeholder="请输入密码" label-align="top" />
            <a href="#/forget" class="forget-pwd">忘记密码</a>
            <div class="btn-1 btn-1-main" @click="loginSubmit()">登录</div>
            <div class="flex-nowrap login-imgs">
                <div class="login-img">
                    <img src="/statics/home/images/pay-wx.png" alt="微信登录" @click="authLogin()" />
                </div>
                <div class="login-img">
                    <img src="/statics/home/images/pay-alipay.png" alt="微信登录" @click="authLogin()" />
                </div>
            </div>
        </div>
    </div>
</template>


<script>
/**
 * 配合 Android 使用时的初始化方法
 */
 const androidFunction = (callback) => {
    if (window.WebViewJavascriptBridge) {
        callback(window.WebViewJavascriptBridge);
    } else {
        document.addEventListener('WebViewJavascriptBridgeReady', function () {
            callback(window.WebViewJavascriptBridge);
        }, false)
    }
}
window.setupWebViewJavascriptBridge = androidFunction;

// 注册 H5 界面的默认接收函数（与安卓交互时，安卓端可以不调用函数名，直接 send 数据过来，就能够在这里接收到数据）
window.setupWebViewJavascriptBridge(function (bridge) {
    bridge.init(function (msg, responseCallback) {
        alert(msg);
        responseCallback("JS 返回给原生的消息内容");
    })
});
/**
 * 调用原生的默认接受方法
 */
const handleInit = () => {
    window.setupWebViewJavascriptBridge(bridge => {
        bridge.send("JS 传递给原生的消息", (data) => {
            message.success(data);
        })
    })
};

// 注册与原生交互的事件函数
window.setupWebViewJavascriptBridge(bridge => {
    /**
     * data：原生传过来的数据
     * fn: 原生传过来的回调函数
     */
    bridge.registerHandler("H5Function", (data, fn) => {
        alert(data);
        fn && fn();
    });
});
import { LOGIN } from '../.././store/mutation_types'
export default {
    name: 'Login',
    data() {
        return {
            user: {
                username: 'gameuser1',
                password: '123123'
            },
            auth:{
                auth_type:1,
                openid:'wxdfsa0.7759499524242692'
            },
        }
    },
    created() { },
    methods: {
        authLogin(){
            this.$api.login.auth(this.auth).then(res => {
                if (res.result.register == 1) {
                    this.$router.push('/register');
                }else{
                    // 保存token
                    let userInfo = {
                        id:res.result.id,
                        name:res.result.username,
                        token:res.result.token,
                    };
                    this.$store.commit(LOGIN, userInfo);
                    if(res.result.certFlag == 1){
                        this.$router.push('/cert');
                    }else {
                        /*
                        /* let data = {
                            'code':200,
                            'gameuser':'test1'
                        };
                        // 调用原生注册的事件函数
                        window.setupWebViewJavascriptBridge(bridge => {
                            bridge.callHandler('getWebData', data, (result) => {
                                alert(result);
                            });
                        }) */
                        this.$router.push('/');
                    }
                }
            });
        },
        // 验证输入
        loginSubmit() {
            this.$api.login.login(this.user).then(res => {
                // 保存token
                let userInfo = {
                    id:res.result.id,
                    name:res.result.username,
                    token:res.result.token,
                };
                this.$store.commit(LOGIN, userInfo);
                if(res.result.certFlag == 1){
                    this.$router.push('/cert');
                }else {
                    /*
                    /* let data = {
                        'code':200,
                        'gameuser':'test1'
                    };
                    // 调用原生注册的事件函数
                    window.setupWebViewJavascriptBridge(bridge => {
                        bridge.callHandler('getWebData', data, (result) => {
                            alert(result);
                        });
                    }) */
                    this.$router.push('/');
                }
            });
        }
    }
}
</script>
